`
https://leetcode.cn/problems/longest-consecutive-sequence/
`

/**
 * @param {number[]} nums
 * @return {number}
 */
var longestConsecutive = function (nums) {
  // 把所有数都存到 Set 里，方便查询
  const set = new Set(nums)

  let res = 0

  for (const num of set) {
    // 如果有 num - 1 的值，说明该 num 并不是连续子序列的第一个
    if (set.has(num - 1)) {
      continue
    }

    let curNum = num
    let curLen = 1

    // 找到最长连续子序列的长度
    while (set.has(curNum + 1)) {
      curNum += 1
      curLen += 1
    }

    // 更新最大值
    res = Math.max(res, curLen)
  }

  return res
};